package main_test

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strings"
)

func Main4() {
	// Ollama 服务地址
	url := "http://localhost:11434/api/chat"

	// 创建请求体
	requestBody := map[string]interface{}{
		"model":  "huihui_ai/deepseek-r1-abliterated:8b",
		"stream": true,
		"messages": []map[string]interface{}{
			{
				"role":    "user",
				"content": "为什么草是绿的？",
			},
		},
	}

	// 将结构体转换为 JSON
	jsonData, err := json.Marshal(requestBody)
	if err != nil {
		fmt.Println("JSON 编码错误:", err)
		return
	}

	// 创建 HTTP 请求
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		fmt.Println("创建请求失败:", err)
		return
	}

	// 设置请求头
	// req.Header.Set("Content-Type", "application/json")

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("请求失败:", err)
		return
	}
	defer resp.Body.Close()

	// 检查响应状态码
	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(resp.Body)
		fmt.Printf("错误响应: %s\n状态码: %d\n", body, resp.StatusCode)
		return
	}

	// 创建读取器，并循环读取响应数据
	reader := bufio.NewReader(resp.Body)
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			if err == io.EOF {
				fmt.Println("io.EOF: ", err)
				break
			}
			break
		}
		line = strings.TrimSpace(line)
		fmt.Println("line: ", line)
	}
}
